/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DlgListCategories.java
 *
 * Created on 2011-04-09, 15:04:28
 */
package dialogs;

import entities.Categorie;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import models.CategoriesTableModel;

/**
 *
 * @author chocoseb
 */
public class DlgListCategories extends javax.swing.JDialog {

    private CategoriesTableModel _model;

    /** Creates new form DlgListCategories */
    public DlgListCategories(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        _model = new CategoriesTableModel();
        tblListCategories.setModel(_model);
        tblListCategories.setDefaultRenderer(Categorie.class, new RendererCategories());
        tblListCategories.setDefaultRenderer(String.class, new RendererCategories());
        this.setLocationRelativeTo(parent);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblListCategories = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        btnAdd = new javax.swing.JButton();
        btnModify = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Categories options");

        tblListCategories.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblListCategories);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        jPanel1.add(btnAdd);

        btnModify.setText("Modify");
        btnModify.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModifyActionPerformed(evt);
            }
        });
        jPanel1.add(btnModify);

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        jPanel1.add(btnDelete);

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });
        jPanel1.add(btnClose);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        this.setVisible(false);
        Categorie.saveAll(_model.getList());
    }//GEN-LAST:event_btnCloseActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        DlgAddCategorie dlg = new DlgAddCategorie(null, true, _model.getList());
        dlg.setVisible(true);
        if (dlg.isConfirmed()) {
            Categorie c = dlg.getNewCategorie();
            _model.add(c);
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        int index = tblListCategories.getSelectedRow();
        _model.delete(index);
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnModifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModifyActionPerformed
        int index = tblListCategories.getSelectedRow();
        if (index >= 0) {
            DlgAddCategorie dlg = new DlgAddCategorie(null, true, _model.getList(), _model.get(index));
            dlg.setVisible(true);
            if (dlg.isConfirmed()) {
                Categorie c = dlg.getUpdatedCategorie(_model.get(index));
                _model.update();
            }
        }

    }//GEN-LAST:event_btnModifyActionPerformed

    public class RendererCategories extends DefaultTableCellRenderer {

        public RendererCategories() {
            super();
        }

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            JLabel laLabel = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if (value instanceof Categorie) {
                Categorie c = (Categorie) value;
                Color temp = c.getColor();
                laLabel.setForeground(Color.WHITE);

                laLabel.setHorizontalAlignment(CENTER);
                laLabel.setText(c.getName());
                laLabel.setFont(new Font(null, Font.BOLD, 12));
                laLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
                if (isSelected) {
                    laLabel.setBackground(temp.darker());
                } else {
                    laLabel.setBackground(temp);
                }
            } else {
                laLabel.setForeground(Color.BLACK);

                laLabel.setHorizontalAlignment(CENTER);
                laLabel.setText("Aucun");
                laLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
                if (isSelected) {
                    laLabel.setBackground(Color.WHITE.darker());
                } else {
                    laLabel.setBackground(Color.WHITE);
                }
            }

            return laLabel;
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnModify;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblListCategories;
    // End of variables declaration//GEN-END:variables
}
